/*
 * IdeogramComponent.java
 * Prakash Manandhar http://p-manandhar.info
 * Created Dec 24, 2010
 */

package ideogrammaker;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author Prakash Manandhar
 */
public class IdeogramComponent extends JComponent
        implements TableModelListener {

    private IdeogramData model;
    private boolean showScale = true;
    private float scaleFontSize = 10.0f;
    private float bar_width = 5.0f;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public IdeogramComponent () {
        init(new IdeogramData());
    }

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public IdeogramComponent (IdeogramData m) {
        init (m);
    }

    protected void init(IdeogramData m) {
        UIManager.put("ideogrammaker.IdeogramUI", "ideogrammaker.BasicIdeogramUI");
        model = m;
        model.addTableModelListener(this);
        calculateAndSetPreferredSize();
        updateUI();
    }

    public void setUI(IdeogramUI ui) { super.setUI(ui); }

    @Override
    public void updateUI() {
        setUI((IdeogramUI)UIManager.getUI(this));
        invalidate();
    }

   public String getUIClassID() { return "ideogrammaker.IdeogramUI"; }

    public IdeogramData getModel () {
        return model;
    }

    public void setModel (IdeogramData m) {
        IdeogramData old = model;
        if(model != null)
            model.removeTableModelListener(this);
        if (m == null)
            model = new IdeogramData();
        else
            model = m;
        model.addTableModelListener(this);
        firePropertyChange ("model", old, model);
    }

    public void tableChanged(TableModelEvent e) {
        calculateAndSetPreferredSize();
        repaint();
    }

    public boolean isFocusable() { return false; }
    public boolean isRequestFocusEnabled() { return false; }
    public boolean isFocusCycleRoot() { return false; }
    public boolean isOpaque() { return true; }

    private void calculateAndSetPreferredSize() {
        
    }

    void save() {
        BasicIdeogramUI bui = (BasicIdeogramUI)(this.ui);
        BufferedImage bi = new BufferedImage(this.getWidth(), this.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        bui.paintGraphics(g, model, getWidth(), getHeight());
        //Create a file chooser
        final JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "PNG Images", "png");
        fc.setFileFilter(filter);

        Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
        fc.setCurrentDirectory(new File(prefs.get("filelocation", "")));
        int returnVal = fc.showSaveDialog(this);
        prefs.put("filelocation", fc.getCurrentDirectory().getAbsolutePath());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File outputfile = fc.getSelectedFile();
            if (!outputfile.getName().endsWith(".png"))
            outputfile = new File(outputfile.getAbsoluteFile() + ".png");
            if (outputfile.exists()) {
                int exists_option = JOptionPane.showConfirmDialog(
                    this,
                    outputfile.getName() + " already exists!\n"
                    + "Do you want to replace?");
                if (exists_option != JOptionPane.YES_OPTION)
                    return;
                        
            }
            try {
                ImageIO.write(bi, "png", outputfile);
            } catch (IOException e) { }
        }
    }
    
}
